package org.jeecg.modules.payment.custom.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.crypto.digest.DigestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.payment.PaymentConfiguration;
import org.jeecg.modules.payment.paymentalipayaccounts.entity.PaymentAlipayAccounts;
import org.jeecg.modules.payment.paymentalipayaccounts.service.IPaymentAlipayAccountsService;
import org.jeecg.modules.payment.paymentalipayorder.service.IPaymentAlipayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * @Description: 支付宝账号
 * @Author: jeecg-boot
 * @Date: 2020-10-07
 * @Version: V1.0
 */
@Api(tags = "支付宝账号")
@RestController("PaymentAlipayAccountsController1")
@RequestMapping("/paymentalipayaccounts/paymentAlipayAccounts")
@Slf4j
public class PaymentAlipayAccountsController extends JeecgController<PaymentAlipayAccounts, IPaymentAlipayAccountsService> {
    @Autowired
    private IPaymentAlipayAccountsService paymentAlipayAccountsService;
    @Autowired
    private IPaymentAlipayOrderService paymentAlipayOrderService;
    @Autowired
    private RedisTemplate redisTemplate;

    @AutoLog(value = "支付宝账号-获取账号")
    @ApiOperation(value = "支付宝账号-获取账号", notes = "支付宝账号-获取账号")
    @PostMapping(value = "/getByAccounts")
    public Result<?> getByAccounts(@RequestBody PaymentAlipayAccounts paymentAlipayAccounts) {
        PaymentAlipayAccounts one = paymentAlipayAccountsService.getOne(paymentAlipayAccountsService.lambdaQuery().eq(PaymentAlipayAccounts::getAccounts, paymentAlipayAccounts.getAccounts()).getWrapper());
        return Result.OK(one);
    }

    @AutoLog(value = "支付宝账号-回调")
    @ApiOperation(value = "支付宝账号-回调", notes = "支付宝账号-回调")
    @GetMapping(value = "/callback")
    public Result<?> getByAccounts(HttpServletRequest httpServletRequest) {
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        TreeMap<String, String[]> treeMap = MapUtil.sort(parameterMap);
        String signature = treeMap.remove("signature")[0];
        StringBuffer stringBuffer = new StringBuffer();
        Set<Map.Entry<String, String[]>> set = treeMap.entrySet();
        set.forEach(entry -> {
            stringBuffer.append("&" + entry.getKey() + "=" + entry.getValue()[0]);
        });
        String key = stringBuffer.substring(1) + "59915298bc564e4d889503034c629d50";
        String signatureMy = DigestUtil.sha256Hex(key.getBytes());
        if (signatureMy.equals(signature)) {
            return Result.OK("Y");
        } else {
            return Result.error("N");
        }

    }

    /**
     * 添加
     *
     * @param paymentAlipayAccounts
     * @return
     */
    @AutoLog(value = "支付宝账号-添加")
    @ApiOperation(value = "支付宝账号-添加", notes = "支付宝账号-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody PaymentAlipayAccounts paymentAlipayAccounts) {
        paymentAlipayAccountsService.save(paymentAlipayAccounts);
        PaymentConfiguration.threadPoolTaskExecutor.execute(() -> {
            PaymentConfiguration.runChromeDriver(PaymentConfiguration.chromeOptions, paymentAlipayAccounts, paymentAlipayAccountsService, paymentAlipayOrderService, redisTemplate);
        });
        return Result.OK("添加成功！");
    }
}
